//
//  XKForgetPwdView.m
//  xk100
//
//  Created by xiaokang on 2017/6/6.
//  Copyright © 2017年 xiaokang. All rights reserved.
//

#import "XKForgetPwdView.h"
@interface XKForgetPwdView () <UITextFieldDelegate>

@property (nonatomic, strong) UIView *backgroundView_one;
@property (nonatomic, strong) UIView *backgroundView_two;
@property (nonatomic, strong) UIView *backgroundView_three;
@property (nonatomic, strong) UIView *backgroundView_four;

@property (nonatomic, strong) UILabel *phoneTitleLabel;
@property (nonatomic, strong) UILabel *vcodeTitleLabel;
@property (nonatomic, strong) UILabel *passwordTitleLabel_one;
@property (nonatomic, strong) UILabel *passwordTitleLabel_two;

@property (nonatomic, strong) UITextField *phoneTextField;
@property (nonatomic, strong) UITextField *vcodeTextField;
@property (nonatomic, strong) UITextField *passwordTextField_one;
@property (nonatomic, strong) UITextField *passwordTextField_two;

@property (nonatomic, strong) UIButton *resetButton;

@end
@implementation XKForgetPwdView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubviews];
    }
    return self;
}

#pragma mark -
- (void)initSubviews {
    _backgroundView_one = [UIView new];
    _backgroundView_one.backgroundColor = WhiteColor;
    
    _backgroundView_two = [UIView new];
    _backgroundView_two.backgroundColor = WhiteColor;
    
    _backgroundView_three = [UIView new];
    _backgroundView_three.backgroundColor = WhiteColor;
    
    _backgroundView_four = [UIView new];
    _backgroundView_four.backgroundColor = WhiteColor;
    
    [self addSubview:_backgroundView_one];
    [self addSubview:_backgroundView_two];
    [self addSubview:_backgroundView_three];
    [self addSubview:_backgroundView_four];
    
    [_backgroundView_one mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(50*Scale_W);
        make.top.mas_equalTo(self).offset(20*Scale_W);
    }];
    
    [_backgroundView_two mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(_backgroundView_one);
        make.top.mas_equalTo(_backgroundView_one.mas_bottom).offset(1);
    }];
    
    [_backgroundView_three mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(_backgroundView_one);
        make.top.mas_equalTo(_backgroundView_two.mas_bottom).offset(1);
    }];
    
    [_backgroundView_four mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(_backgroundView_one);
        make.top.mas_equalTo(_backgroundView_three.mas_bottom).offset(1);
    }];
    
    
    
    _phoneTitleLabel = [UILabel getLabWithText:@"手  机  号:" textFont:Font_15 textColor:[UIColor darkGrayColor]];
    _vcodeTitleLabel = [UILabel getLabWithText:@"验  证  码:" textFont:Font_15 textColor:[UIColor darkGrayColor]];
    _passwordTitleLabel_one = [UILabel getLabWithText:@"重置密码:" textFont:Font_15 textColor:[UIColor darkGrayColor]];
    _passwordTitleLabel_two = [UILabel getLabWithText:@"确认密码:" textFont:Font_15 textColor:[UIColor darkGrayColor]];
    
    [_backgroundView_one addSubview:_phoneTitleLabel];
    [_backgroundView_two addSubview:_vcodeTitleLabel];
    [_backgroundView_three addSubview:_passwordTitleLabel_one];
    [_backgroundView_four addSubview:_passwordTitleLabel_two];
    
    [_phoneTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backgroundView_one).offset(30*Scale_W);
        make.top.bottom.mas_equalTo(_backgroundView_one);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [_vcodeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backgroundView_two).offset(30*Scale_W);
        make.top.bottom.mas_equalTo(_backgroundView_two);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [_passwordTitleLabel_one mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backgroundView_three).offset(30*Scale_W);
        make.top.bottom.mas_equalTo(_backgroundView_three);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [_passwordTitleLabel_two mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backgroundView_four).offset(30*Scale_W);
        make.top.bottom.mas_equalTo(_backgroundView_four);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    _phoneTextField = [UITextField new];
    _phoneTextField.delegate = self;
    _phoneTextField.font = Font(15);
    _phoneTextField.borderStyle = UITextBorderStyleNone;
    _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    _phoneTextField.placeholder = @"请输入11位手机号";
    [_phoneTextField addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _vcodeTextField = [UITextField new];
    _vcodeTextField.delegate = self;
    _vcodeTextField.font = Font(15);
    _vcodeTextField.borderStyle = UITextBorderStyleNone;
    _vcodeTextField.keyboardType = UIKeyboardTypeNumberPad;
    _vcodeTextField.placeholder = @"请输入6位验证码";
    [_vcodeTextField addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _passwordTextField_one = [UITextField new];
    _passwordTextField_one.delegate = self;
    _passwordTextField_one.font = Font(15);
    _passwordTextField_one.secureTextEntry = YES;
    _passwordTextField_one.borderStyle = UITextBorderStyleNone;
    _passwordTextField_one.placeholder = @"请输入6-16位新登录密码";
    [_passwordTextField_one addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _passwordTextField_two = [UITextField new];
    _passwordTextField_two.delegate = self;
    _passwordTextField_two.font = Font(15);
    _passwordTextField_two.secureTextEntry = YES;
    _passwordTextField_two.borderStyle = UITextBorderStyleNone;
    _passwordTextField_two.placeholder = @"请确认新登录密码";
    [_passwordTextField_two addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    
    [_backgroundView_one addSubview:_phoneTextField];
    [_backgroundView_two addSubview:_vcodeTextField];
    [_backgroundView_three addSubview:_passwordTextField_one];
    [_backgroundView_four addSubview:_passwordTextField_two];
    
    [_phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_phoneTitleLabel.mas_right);
        make.top.bottom.mas_equalTo(_backgroundView_one);
        make.right.mas_equalTo(_backgroundView_one).offset(-30*Scale_W);
    }];
    
    [_vcodeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_vcodeTitleLabel.mas_right);
        make.top.bottom.mas_equalTo(_backgroundView_two);
        make.right.mas_equalTo(_backgroundView_two).offset(-130*Scale_W);
    }];
    
    [_passwordTextField_one mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_passwordTitleLabel_one.mas_right);
        make.top.bottom.mas_equalTo(_backgroundView_three);
        make.right.mas_equalTo(_backgroundView_three).offset(-30*Scale_W);
    }];
    
    [_passwordTextField_two mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_passwordTitleLabel_two.mas_right);
        make.top.bottom.mas_equalTo(_backgroundView_four);
        make.right.mas_equalTo(_backgroundView_four).offset(-30*Scale_W);
    }];
    
    
    _vcodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _vcodeButton.enabled = NO;
    _vcodeButton.titleLabel.font = Font(15);
    _vcodeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [_vcodeButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_vcodeButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [_vcodeButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [_vcodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_vcodeButton addTarget:self action:@selector(vcodeButtonTouchEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    [_backgroundView_two addSubview:_vcodeButton];
    
    [_vcodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_vcodeTextField.mas_right);
        make.top.bottom.mas_equalTo(_backgroundView_two);
        make.right.mas_equalTo(_backgroundView_two).offset(-30*Scale_W);
    }];
    
    _resetButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _resetButton.enabled = NO;
    _resetButton.layer.cornerRadius = 5*Scale_W;
    _resetButton.layer.masksToBounds = YES;
    _resetButton.titleLabel.font = Font(15);
    [_resetButton setTitle:@"重置密码" forState:UIControlStateNormal];
    [_resetButton setBackgroundImage:[UIImage imageWithColor:LightGrayColor] forState:UIControlStateDisabled];
    [_resetButton setBackgroundImage:[UIImage imageWithColor:LightGrayColor] forState:UIControlStateSelected];
    [_resetButton setBackgroundImage:[UIImage imageWithColor:kBaseRedColor] forState:UIControlStateNormal];
    [_resetButton setTitleColor:WhiteColor forState:UIControlStateNormal];
    [_resetButton setTitleColor:GrayColor forState:UIControlStateDisabled];
    [_resetButton setTitleColor:GrayColor forState:UIControlStateSelected];
    [_resetButton addTarget:self action:@selector(resetButtonTouchEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_resetButton];
    
    [_resetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30*Scale_W);
        make.right.mas_equalTo(-30*Scale_W);
        make.height.mas_equalTo(40*Scale_W);
        make.top.mas_equalTo(_backgroundView_four.mas_bottom).offset(20*Scale_W);
    }];
    
}

- (void)textFieldDidChanged:(UITextField *)textField {
    
    NSString *timeOut_get = [NSString calculateNumberOne:[[NSUserDefaults standardUserDefaults] objectForKey:@"timeOut_forgetpwd"] calculateType:CalculateType_Subtract numberTwo:[NSString getTimestamp]];
    
    if (textField == _phoneTextField) {
        if (_phoneTextField.text.length == 11 && timeOut_get.integerValue < 0) {
            _vcodeButton.enabled = YES;
        } else{
            _vcodeButton.enabled = NO;
        }
    }
    
    if (_passwordTextField_one.text.length >= 6 &&
        _passwordTextField_two.text.length >= 6 &&
        _vcodeTextField.text.length == 6 &&
        _phoneTextField.text.length == 11) {
        _resetButton.enabled = YES;
    } else {
        _resetButton.enabled = NO;
    }
}

#pragma mark - 控制字符输入长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSMutableString *text = [NSMutableString stringWithString:textField.text];
    [text replaceCharactersInRange:range withString:string];
    if (textField == _phoneTextField) {
        return (text.length > 11 || ![string isOnlyNumber]) ? NO : YES;
    } else if (textField == _vcodeTextField) {
        return (text.length > 6 || ![string isOnlyNumber]) ? NO : YES;
    } else if (textField == _passwordTextField_one || textField == _passwordTextField_two) {
        return text.length > 16 ? NO : YES;
    }
    return YES;
}

#pragma mark -
- (void)vcodeButtonTouchEvent:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(getvcodeWithPhone:)]) {
        [self.delegate getvcodeWithPhone:_phoneTextField.text];
    }
}

#pragma mark -
- (void)resetButtonTouchEvent:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(resetPasswordWithPhone:vcode:password_new:password_confirm:)]) {
        [self.delegate resetPasswordWithPhone:_phoneTextField.text vcode:_vcodeTextField.text password_new:_passwordTextField_one.text password_confirm:_passwordTextField_two.text];
    }
}

- (void)setPhoneString:(NSString *)phoneString {
    _phoneString = phoneString;
    _phoneTextField.text = phoneString;
    _vcodeButton.enabled = [phoneString isMobileNumber];
}

@end
